package me.heshang.rednamedrop;

import me.heshang.rednamedrop.Command.Command;
import me.heshang.rednamedrop.Connection.MySql;
import me.heshang.rednamedrop.Placeholder.Placeholder;
import org.bukkit.*;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.sql.*;

public final class RedNamedrop extends JavaPlugin implements Listener {

    private static RedNamedrop plugin;
    private static String Message_redincrease;
    private static String Message_redreduce;
    private static String Message_redclear;
    private static String Message_redlevel;
    private static String Message_redworld;
    private static String Message_addprompt_success;
    private static String Message_addprompt_fail;
    private static String Message_deductionprompt_success;
    private static String Message_deductionprompt_fail;
    private static String Message_deductionprompt_exceed;

    @Override
    public void onEnable() {
        plugin = this;
        this.getConfig().options().copyDefaults();
        this.saveDefaultConfig();
        this.onLoadConfig();
        new MySql().getConnection();
        this.Mysql();
        if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
            Bukkit.getPluginManager().registerEvents(this, this);
            new Placeholder(this).register();
            Bukkit.getConsoleSender().sendMessage("§b|> §e检测到PlaceholderAPI");

        } else {
            Bukkit.getConsoleSender().sendMessage("§b========= §eRedNamedrop §b=========");
            Bukkit.getConsoleSender().sendMessage("§b|> §e找不到PlaceholderAPI！此插件是必需的。");
            Bukkit.getPluginManager().disablePlugin(this);
        }
        Bukkit.getConsoleSender().sendMessage("§b========= §eRedNamedrop §b=========");
        Bukkit.getConsoleSender().sendMessage("§b|> §eRedNamedrop插件已启用");
        Bukkit.getConsoleSender().sendMessage("§b|> §e作者: 小和尚");
        Bukkit.getConsoleSender().sendMessage("§b|> §e版本: §e免费版");
        Bukkit.getConsoleSender().sendMessage("§b|> §e收费版将开通更多功能");
        Bukkit.getConsoleSender().sendMessage("§b|> §e联系: 317808305");
        getServer().getPluginManager().registerEvents(new Drop(),this);
        Bukkit.getPluginManager().registerEvents(this,this);
        getServer().getPluginManager().registerEvents(new PlayerkilTitle(),this);
        Bukkit.getPluginManager().registerEvents(this,this);
        getServer().getPluginManager().registerEvents(new RedWorld(),this);
        Bukkit.getPluginManager().registerEvents(this,this);
        getServer().getPluginManager().registerEvents(new PlayerJoin(),this);
        Bukkit.getPluginManager().registerEvents(this,this);


        getCommand("rnd").setExecutor(new Command());


    }

    @Override
    public void onDisable(){
        //关闭连接
        try {
            MySql.getShut();
            Bukkit.getConsoleSender().sendMessage("§b[RedNamedrop]> §e数据库已关闭");
        } catch (SQLException e) {
            e.printStackTrace();
        }


        Bukkit.getConsoleSender().sendMessage("§b========= §eRedNamedrop §b=========");
        Bukkit.getConsoleSender().sendMessage("§b|> §eRedNamedrop插件已卸载");
        Bukkit.getConsoleSender().sendMessage("§b|> §e作者: 小和尚");
        Bukkit.getConsoleSender().sendMessage("§b|> §e版本: §e免费版");
        Bukkit.getConsoleSender().sendMessage("§b|> §e收费版将开通更多功能");
        Bukkit.getConsoleSender().sendMessage("§b|> §e联系: 317808305")   ;

    }

    public void onLoadConfig(){

        new DropItem().Count = this.getConfig().getInt("count");
        new PlayerkilTitle().setBiologykill(this.getConfig().getBoolean("Biological.biologykill"));
        new PlayerkilTitle().setQuantity(this.getConfig().getInt("Biological.quantity"));
        new Drop().World = this.getConfig().getStringList("world");
        new Drop().Lever = this.getConfig().getInt("lever");
        new MySql().setIp(this.getConfig().getString("mysql.ip"));
        new RedWorld().setRedWorld(this.getConfig().getString("RedWorld.World"));
        new RedWorld().setTime(this.getConfig().getInt("RedWorld.Time"));
        new MySql().setPort(this.getConfig().getString("mysql.port"));
        new MySql().setDatabasename(this.getConfig().getString("mysql.databasename"));
        new MySql().setUsername(this.getConfig().getString("mysql.username"));
        new MySql().setPassworld(this.getConfig().getString("mysql.passworld"));
        new MySql().setTabName(this.getConfig().getString("mysql.tabName"));
        new Placeholder().setByrnd(this.getConfig().getString("statusBoolean.byrnd"));
        new Placeholder().setNotrnd(this.getConfig().getString("statusBoolean.Notrnd"));
        Message_redincrease = this.getConfig().getString("Message_redincrease");
        Message_redreduce = this.getConfig().getString("Message_redreduce");
        Message_redclear = this.getConfig().getString("Message_redclear");
        Message_redlevel = this.getConfig().getString("Message_redlevel");
        Message_redworld = this.getConfig().getString("Message_redworld");
        Message_addprompt_success = this.getConfig().getString("Message_addprompt_success");
        Message_addprompt_fail = this.getConfig().getString("Message_addprompt_fail");
        Message_deductionprompt_success = this.getConfig().getString("Message_deductionprompt_success");
        Message_deductionprompt_fail = this.getConfig().getString("Message_deductionprompt_fail");
        Message_deductionprompt_exceed = this.getConfig().getString("Message_deductionprompt_exceed");
        new Command().setMessage_reload(this.getConfig().getString("Message_reload"));
        new Command().setMessage_addlevelerror(this.getConfig().getString("Message_addlevelerror"));
        new Command().setMessage_removelevelerror(this.getConfig().getString("Message_removelevelerror"));
        new Command().setMessage_errorprompt(this.getConfig().getString("Message_errorprompt"));
        new Command().setMessage_querylevelerror(this.getConfig().getString("Message_querylevelerror"));
        new Command().setMessage_help(this.getConfig().getStringList("Message_help"));

    }

    public void Mysql() {
        new MySql().getConnection();
        MySql.setUrl("jdbc:mysql://"+MySql.getIp()+":"+MySql.getPort()+"/"+MySql.getDatabasename()+"?useSSL=false");
        new BukkitRunnable() {

            @Override
            public void run() {
                //加载驱动类
                try {

                    Class.forName("com.mysql.jdbc.Driver");
                    MySql.setConnection(DriverManager.getConnection(MySql.getUrl(), MySql.getUsername(), MySql.getPassworld()));
                    MySql.setSt(MySql.getConnection().createStatement());

                    if (MySql.getConnection() == null){
                        System.out.println("数据库连接失败请检查配置是否正确");
                        Bukkit.getConsoleSender().sendMessage("§b[RedNamedrop]]> §e数据库连接失败请检查配置是否正确");
                        Bukkit.getConsoleSender().sendMessage("§b[RedNamedrop]]> §e插件将关闭");
                        new RedNamedrop().onDisable();
                    }else {
                        Bukkit.getConsoleSender().sendMessage("§b[RedNamedrop]]> §e数据库连接成功");

                        new MySql().getConnection().close();
                    }

                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }


            }
        }.runTaskAsynchronously((Plugin) this);
    }

    public static RedNamedrop getPlugin() {
        return plugin;
    }

    public static void setPlugin(RedNamedrop plugin) {
        RedNamedrop.plugin = plugin;
    }

    public static String getMessage_redincrease() {
        return Message_redincrease;
    }

    public static void setMessage_redincrease(String message_redincrease) {
        Message_redincrease = message_redincrease;
    }

    public static String getMessage_redreduce() {
        return Message_redreduce;
    }

    public static void setMessage_redreduce(String message_redreduce) {
        Message_redreduce = message_redreduce;
    }

    public static String getMessage_redclear() {
        return Message_redclear;
    }

    public static void setMessage_redclear(String message_redclear) {
        Message_redclear = message_redclear;
    }

    public static String getMessage_redlevel() {
        return Message_redlevel;
    }

    public static void setMessage_redlevel(String message_redlevel) {
        Message_redlevel = message_redlevel;
    }

    public static String getMessage_redworld() {
        return Message_redworld;
    }

    public static void setMessage_redworld(String message_redworld) {
        Message_redworld = message_redworld;
    }

    public static String getMessage_addprompt_success() {
        return Message_addprompt_success;
    }

    public static void setMessage_addprompt_success(String message_addprompt_success) {
        Message_addprompt_success = message_addprompt_success;
    }

    public static String getMessage_addprompt_fail() {
        return Message_addprompt_fail;
    }

    public static void setMessage_addprompt_fail(String message_addprompt_fail) {
        Message_addprompt_fail = message_addprompt_fail;
    }

    public static String getMessage_deductionprompt_success() {
        return Message_deductionprompt_success;
    }

    public static void setMessage_deductionprompt_success(String message_deductionprompt_success) {
        Message_deductionprompt_success = message_deductionprompt_success;
    }

    public static String getMessage_deductionprompt_fail() {
        return Message_deductionprompt_fail;
    }

    public static void setMessage_deductionprompt_fail(String message_deductionprompt_fail) {
        Message_deductionprompt_fail = message_deductionprompt_fail;
    }

    public static String getMessage_deductionprompt_exceed() {
        return Message_deductionprompt_exceed;
    }

    public static void setMessage_deductionprompt_exceed(String message_deductionprompt_exceed) {
        Message_deductionprompt_exceed = message_deductionprompt_exceed;
    }
}
